// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Core;
using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.IndexManagement;

public partial class SettingsSimilarities : IsADictionary<string, ISettingsSimilarity>
{
	public SettingsSimilarities()
	{
	}

	public SettingsSimilarities(IDictionary<string, ISettingsSimilarity> container) : base(container)
	{
	}

	public void Add(string name, ISettingsSimilarity settingsSimilarity) => BackingDictionary.Add(Sanitize(name), settingsSimilarity);
	public bool TryGetSettingsSimilarity(string name, [NotNullWhen(returnValue: true)] out ISettingsSimilarity settingsSimilarity) => BackingDictionary.TryGetValue(Sanitize(name), out settingsSimilarity);

	public bool TryGetSettingsSimilarity<T>(string name, [NotNullWhen(returnValue: true)] out T? settingsSimilarity) where T : class, ISettingsSimilarity
	{
		if (BackingDictionary.TryGetValue(Sanitize(name), out var matchedValue) && matchedValue is T finalValue)
		{
			settingsSimilarity = finalValue;
			return true;
		}

		settingsSimilarity = null;
		return false;
	}
}

public sealed partial class SettingsSimilaritiesDescriptor : IsADictionaryDescriptor<SettingsSimilaritiesDescriptor, SettingsSimilarities, string, ISettingsSimilarity>
{
	public SettingsSimilaritiesDescriptor() : base(new SettingsSimilarities())
	{
	}

	public SettingsSimilaritiesDescriptor(SettingsSimilarities settingsSimilarities) : base(settingsSimilarities ?? new SettingsSimilarities())
	{
	}

	public SettingsSimilaritiesDescriptor Bm25(string settingsSimilarityName) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBm25Descriptor, SettingsSimilarityBm25>(settingsSimilarityName, null);
	public SettingsSimilaritiesDescriptor Bm25(string settingsSimilarityName, Action<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBm25Descriptor> configure) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBm25Descriptor, SettingsSimilarityBm25>(settingsSimilarityName, configure);
	public SettingsSimilaritiesDescriptor Bm25(string settingsSimilarityName, SettingsSimilarityBm25 settingsSimilarityBm25) => AssignVariant(settingsSimilarityName, settingsSimilarityBm25);
	public SettingsSimilaritiesDescriptor Boolean(string settingsSimilarityName) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBooleanDescriptor, SettingsSimilarityBoolean>(settingsSimilarityName, null);
	public SettingsSimilaritiesDescriptor Boolean(string settingsSimilarityName, Action<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBooleanDescriptor> configure) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBooleanDescriptor, SettingsSimilarityBoolean>(settingsSimilarityName, configure);
	public SettingsSimilaritiesDescriptor Boolean(string settingsSimilarityName, SettingsSimilarityBoolean settingsSimilarityBoolean) => AssignVariant(settingsSimilarityName, settingsSimilarityBoolean);
	public SettingsSimilaritiesDescriptor Dfi(string settingsSimilarityName) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfiDescriptor, SettingsSimilarityDfi>(settingsSimilarityName, null);
	public SettingsSimilaritiesDescriptor Dfi(string settingsSimilarityName, Action<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfiDescriptor> configure) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfiDescriptor, SettingsSimilarityDfi>(settingsSimilarityName, configure);
	public SettingsSimilaritiesDescriptor Dfi(string settingsSimilarityName, SettingsSimilarityDfi settingsSimilarityDfi) => AssignVariant(settingsSimilarityName, settingsSimilarityDfi);
	public SettingsSimilaritiesDescriptor Dfr(string settingsSimilarityName) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfrDescriptor, SettingsSimilarityDfr>(settingsSimilarityName, null);
	public SettingsSimilaritiesDescriptor Dfr(string settingsSimilarityName, Action<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfrDescriptor> configure) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfrDescriptor, SettingsSimilarityDfr>(settingsSimilarityName, configure);
	public SettingsSimilaritiesDescriptor Dfr(string settingsSimilarityName, SettingsSimilarityDfr settingsSimilarityDfr) => AssignVariant(settingsSimilarityName, settingsSimilarityDfr);
	public SettingsSimilaritiesDescriptor Ib(string settingsSimilarityName) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityIbDescriptor, SettingsSimilarityIb>(settingsSimilarityName, null);
	public SettingsSimilaritiesDescriptor Ib(string settingsSimilarityName, Action<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityIbDescriptor> configure) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityIbDescriptor, SettingsSimilarityIb>(settingsSimilarityName, configure);
	public SettingsSimilaritiesDescriptor Ib(string settingsSimilarityName, SettingsSimilarityIb settingsSimilarityIb) => AssignVariant(settingsSimilarityName, settingsSimilarityIb);
	public SettingsSimilaritiesDescriptor Lmd(string settingsSimilarityName) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmdDescriptor, SettingsSimilarityLmd>(settingsSimilarityName, null);
	public SettingsSimilaritiesDescriptor Lmd(string settingsSimilarityName, Action<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmdDescriptor> configure) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmdDescriptor, SettingsSimilarityLmd>(settingsSimilarityName, configure);
	public SettingsSimilaritiesDescriptor Lmd(string settingsSimilarityName, SettingsSimilarityLmd settingsSimilarityLmd) => AssignVariant(settingsSimilarityName, settingsSimilarityLmd);
	public SettingsSimilaritiesDescriptor Lmj(string settingsSimilarityName) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmjDescriptor, SettingsSimilarityLmj>(settingsSimilarityName, null);
	public SettingsSimilaritiesDescriptor Lmj(string settingsSimilarityName, Action<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmjDescriptor> configure) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmjDescriptor, SettingsSimilarityLmj>(settingsSimilarityName, configure);
	public SettingsSimilaritiesDescriptor Lmj(string settingsSimilarityName, SettingsSimilarityLmj settingsSimilarityLmj) => AssignVariant(settingsSimilarityName, settingsSimilarityLmj);
	public SettingsSimilaritiesDescriptor Scripted(string settingsSimilarityName) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityScriptedDescriptor, SettingsSimilarityScripted>(settingsSimilarityName, null);
	public SettingsSimilaritiesDescriptor Scripted(string settingsSimilarityName, Action<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityScriptedDescriptor> configure) => AssignVariant<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityScriptedDescriptor, SettingsSimilarityScripted>(settingsSimilarityName, configure);
	public SettingsSimilaritiesDescriptor Scripted(string settingsSimilarityName, SettingsSimilarityScripted settingsSimilarityScripted) => AssignVariant(settingsSimilarityName, settingsSimilarityScripted);
}

internal sealed partial class SettingsSimilarityInterfaceConverter : JsonConverter<ISettingsSimilarity>
{
	public override ISettingsSimilarity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		var copiedReader = reader;
		string? type = null;
		using var jsonDoc = JsonDocument.ParseValue(ref copiedReader);
		if (jsonDoc is not null && jsonDoc.RootElement.TryGetProperty("type", out var readType) && readType.ValueKind == JsonValueKind.String)
		{
			type = readType.ToString();
		}

		switch (type)
		{
			case "BM25":
				return JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBm25>(ref reader, options);
			case "boolean":
				return JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBoolean>(ref reader, options);
			case "DFI":
				return JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfi>(ref reader, options);
			case "DFR":
				return JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfr>(ref reader, options);
			case "IB":
				return JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityIb>(ref reader, options);
			case "LMDirichlet":
				return JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmd>(ref reader, options);
			case "LMJelinekMercer":
				return JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmj>(ref reader, options);
			case "scripted":
				return JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityScripted>(ref reader, options);
			default:
				ThrowHelper.ThrowUnknownTaggedUnionVariantJsonException(type, typeof(ISettingsSimilarity));
				return null;
		}
	}

	public override void Write(Utf8JsonWriter writer, ISettingsSimilarity value, JsonSerializerOptions options)
	{
		if (value is null)
		{
			writer.WriteNullValue();
			return;
		}

		switch (value.Type)
		{
			case "BM25":
				JsonSerializer.Serialize(writer, value, typeof(Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBm25), options);
				return;
			case "boolean":
				JsonSerializer.Serialize(writer, value, typeof(Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityBoolean), options);
				return;
			case "DFI":
				JsonSerializer.Serialize(writer, value, typeof(Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfi), options);
				return;
			case "DFR":
				JsonSerializer.Serialize(writer, value, typeof(Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityDfr), options);
				return;
			case "IB":
				JsonSerializer.Serialize(writer, value, typeof(Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityIb), options);
				return;
			case "LMDirichlet":
				JsonSerializer.Serialize(writer, value, typeof(Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmd), options);
				return;
			case "LMJelinekMercer":
				JsonSerializer.Serialize(writer, value, typeof(Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityLmj), options);
				return;
			case "scripted":
				JsonSerializer.Serialize(writer, value, typeof(Elastic.Clients.Elasticsearch.Serverless.IndexManagement.SettingsSimilarityScripted), options);
				return;
			default:
				var type = value.GetType();
				JsonSerializer.Serialize(writer, value, type, options);
				return;
		}
	}
}

[JsonConverter(typeof(SettingsSimilarityInterfaceConverter))]
public partial interface ISettingsSimilarity
{
	public string? Type { get; }
}